﻿/*
 * zrandom_unittest.cxx
 *
 *  Created on: 2015年3月4日
 *      Author: Fifi Lyu
 */

#include <gtest/gtest.h>
#include "zpsoe/zalgorithm/zrandom.h"
#include "zpsoe/zalgorithm/zarray.h"
#include "zpsoe/zalgorithm/ztime.h"

using namespace zpsoe::zalgorithm;

bool check_char_classification(const string &s, char_classification_t cc = CC_PRINT);

bool check_char_classification(const string &s, char_classification_t cc) {
    bool ret_ = true;

    for(uint32_t i_ = 0; i_ < s.size(); ++i_) {
        char c_ = s[i_];

        switch (cc) {
            case CC_ALNUM:
                ret_ = isalnum(c_);
                break;
            case CC_ALPHA:
                ret_ = isalpha(c_);
                break;
            case CC_DIGIT:
                ret_ = isdigit(c_);
                break;
            case CC_PRINT:
                ret_ = (!isspace(c_) && isprint(c_));
                break;
            default:
                break;
        }

        if (!ret_)
            return false;
    }

    return true;
}

TEST(ZrandomTest, get_int64_random) {
    vector<int64_t> v_;

    for (int64_t i_ = 0; i_ < 100; ++i_) {
        ztime::msleep(1);
        v_.push_back(zrandom::get_random());
    }

    const size_t v_size_ = v_.size();

    zarray::unique(v_);
    const size_t v_unique_size_ = v_.size();
    const double ratio_ = v_unique_size_ / 100.0;

    // ratio_ >= 90%
    EXPECT_GE(ratio_, 0.90);
}

TEST(ZrandomTest, gen_str_random) {
    const string s1_(zrandom::gen_random(11, CC_ALNUM));
    EXPECT_EQ(11, s1_.size());
    EXPECT_TRUE(check_char_classification(s1_, CC_ALNUM));

    const string s2_(zrandom::gen_random(12, CC_ALPHA));
    EXPECT_EQ(12, s2_.size());
    EXPECT_TRUE(check_char_classification(s2_, CC_ALPHA));

    const string s3_(zrandom::gen_random(13, CC_DIGIT));
    EXPECT_EQ(13, s3_.size());
    EXPECT_TRUE(check_char_classification(s3_, CC_DIGIT));

    const string s4_(zrandom::gen_random(14, CC_PRINT));
    EXPECT_EQ(14, s4_.size());
    EXPECT_TRUE(check_char_classification(s4_, CC_PRINT));
}
